package com.ziyan.config;

import com.ziyan.cache.CacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

@Component
public class TimerConfig {
    private static final Logger log = LoggerFactory.getLogger(Timer.class);

    @PostConstruct
    public void main() throws Exception {
        //得到时间类
        Calendar date = Calendar.getInstance();
        //设置时间为 xx-xx-xx 00:00:00
        date.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DATE), 0, 0, 0);
        //一天的毫秒数
        long daySpan = 24 * 60 * 60 * 1000;
        //得到定时器实例
        Timer t = new Timer();
        log.info("开启定时任务。");
        t.schedule(new TimerTask() {
            public void run() {
                log.info("执行定时任务，清楚缓存。");
                CacheManager.clearAll();
            }
        }, date.getTime(), daySpan); //daySpan是一天的毫秒数，也是执行间隔
    }

    ;
}